package com.nero.controller;

import com.github.pagehelper.PageInfo;
import com.nero.bean.BaseDataGrid;
import com.nero.bean.PageMessage;
import com.nero.bean.VO.DatagridReceived;
import com.nero.bean.VO.ItemAddVO;
import com.nero.bean.VO.ItemVO;
import com.nero.common.pojo.EUTreeNode;
import com.nero.common.pojo.TaotaoResult;
import com.nero.service.IItemService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Nero on 18/8/6.
 */
@Controller
@RequestMapping("/item")
public class ItemController {
    @Resource
    IItemService itemServiceImpl;

    private static Logger logger = LogManager.getLogger(ItemController.class);

    @RequestMapping("/list")
    @ResponseBody
    public BaseDataGrid list(@RequestParam int page, @RequestParam int rows) {
        logger.info("进行获取产品操作");
        PageInfo pageInfo =  itemServiceImpl.findAllItemVO(page, rows);
        BaseDataGrid baseDataGrid = new BaseDataGrid();
        baseDataGrid.setRows(pageInfo.getList());
        baseDataGrid.setTotal(pageInfo.getTotal());
        logger.info("获取产品操作结束");
        return baseDataGrid;
    }

    @RequestMapping("/cat/list")
    @ResponseBody
    public List<EUTreeNode> catList(@RequestParam(defaultValue = "0") long id){
        logger.info("进行获取列表操作");
        List<EUTreeNode> catlist = itemServiceImpl.findTreeNodes(id);
        logger.info("获取列表操作完毕");
        return catlist;
    }

    @RequestMapping("/param/list")
    @ResponseBody
    public BaseDataGrid paramList(@RequestParam int page, @RequestParam int rows) {
        logger.info("进行获取规格参数操作");
        PageInfo pageInfo =  itemServiceImpl.findAllItemParamVO(page, rows);
        BaseDataGrid baseDataGrid = new BaseDataGrid();
        baseDataGrid.setRows(pageInfo.getList());
        baseDataGrid.setTotal(pageInfo.getTotal());
        logger.info("获取规格参数操作结束");
        return baseDataGrid;
    }

    @RequestMapping("/save")
    @ResponseBody
    public TaotaoResult save(ItemAddVO itemAddVO){
        TaotaoResult result = new TaotaoResult();
        logger.info(itemAddVO.toString());
        itemServiceImpl.addItem(itemAddVO);
        result.setStatus(200);
        return result;
    }

}